<?php
class IndexAction extends BaseAction{
	
	const USER_LOGIN_OK			=	1;		//成功
	const USER_LOGIN_YZM_ERR	=	2;		// 验证码错误
	const USER_LOGIN_NAME_ERR	=	3;		// 用户名错误
	const USER_LOGIN_PWD_ERR	=	4;		// 密码错误
	const USER_LOGIN_AUTHORIZE_ERR	=	5;		// 没有登陆的授权
	
	function index(){
		//message("测试");
		redirect("?g=Mobile&m=Index&a=login");
		exit;
	}
	/**
	 * 登录页面
	 * Enter description here ...
	 */
	function login(){
		$this->assign("title",'佳空间  报装服务终端');
		
		//判断是否自动登录
	   	if($_COOKIE["locationAutonLoginJKJ"]){
	   		
	   		$__userinfo=explode('|', $_COOKIE["locationAutonLoginJKJ"]);
	   		$rev=ExUse::setUser($__userinfo[0],$__userinfo[1]);
	   		if($rev){
	   			$this->redirect("?g=Mobile&m=Index&a=main");
	   		}
	   	}
	   
	   	if($_COOKIE["locationUserJKJ"]){
	   		$__s=$_COOKIE["locationUserJKJ"];
	   		$this->assign("LOCATIONUSER",$__s);
	   	}
		$this->display();
	}
	/**
	 * 主界面
	 * Enter description here ...
	 */
	function main(){
		$this->assign("title",'佳空间  报装服务终端');
		$this->display();
	}
	/**
	 * 用户登录
	 * Enter description here ...
	 */
	function checkuser(){
		/**
		 * 1: 登陆正确
		 * 2：验证码错误
		 * 3：用户错误
		 * 4：密码错误
		 * Enter description here ...
		 * @var unknown_type
		 */
		$userName=$this->in("userName");
		$userPwd=$this->in("userPwd");
		
		$role=$this->in("role");
		$useUser=$this->in("useUser");
		$autologin=$this->in("autologin");
		$user=D("User");
		//会员名/邮箱  
		$IS_USER_OK=false;
		$search["U_NAME"]=$userName;
		$data=$user->where($search)->select();
		if($data){
			/*$pwd=$data[0]["U_PWD"];
			if($pwd!=md5($userPwd)){
				echo self::USER_LOGIN_PWD_ERR;
				exit;
			}
			*/
			$IS_USER_OK=true;
		}else{
			//echo self::USER_LOGIN_NAME_ERR;
			//exit;
			//检查是否邮箱登录
			$user=D("User");
			$search=array();
			$search["U_EMAIL"]=$userName;
			$data=$user->where($search)->select();
			if($data){
				$IS_USER_OK=true;
			}else{
				//检查是否手机登录
				$user=D("User");
				$search=array();
				$search["U_MOBILE"]=$userName;
				$data=$user->where($search)->select();
				if($data){
					$IS_USER_OK=true;
				}
			}
			
		}
		//检查密码
		if($IS_USER_OK && $data){
			$pwd=$data[0]["U_PWD"];
			if($pwd!=md5($userPwd)){
				
				message("密码错误","?g=Mobile&m=Index&a=login");
				/*echo self::USER_LOGIN_PWD_ERR;
				exit;*/
			}
		}else{
			message("登录名错误","?g=Mobile&m=Index&a=login");
			/*
			echo self::USER_LOGIN_NAME_ERR;
			exit;*/
		}
		
		$_user=$data[0];
		
		//取用户的角色
		try{
			$_roles=ExUse::getUserRolesByArr($_user["U_ID"]);
			
			if($_roles && is_array($_roles) && count($_roles)){
				//如果是多角色  先取一个角色使用
				$_user["roleId"]=$_roles[0]["R_ID"];
				$_user["roleName"]=$_roles[0]["R_NAME"];
				$_user["roleShortname"]=$_roles[0]["R_SHORT_NAME"];
			}
		}
		catch (Exception $e){
		
			message("无权登录","?g=Mobile&m=Index&a=login");
			
		}
		if($role=='srv'){
			if($_user["roleShortname"]){
				$__ah=C("ROLE_LOGIN_AUTHORIZE_2");
				//先只对服务队登录限制吧
				if(!in_array($_user["roleShortname"], $__ah)){
					message("无权登录","?g=Mobile&m=Index&a=login");
					/*
					echo self::USER_LOGIN_AUTHORIZE_ERR;
					exit;
					*/
				}
				
			}else{
				
				message("无权登录","?g=Mobile&m=Index&a=login");
				/*
				echo self::USER_LOGIN_AUTHORIZE_ERR;
				exit;*/
			}
		}
		//客户登录界面 服务队不能登录
		if($role=='cr'){
			if($_user["roleShortname"]){
				$__ah=C("ROLE_LOGIN_AUTHORIZE_2");
				//客户登录判断是否服务队角色 
				if(in_array($_user["roleShortname"], $__ah)){
					message("无权登录","?g=Mobile&m=Index&a=login");
					/*echo self::USER_LOGIN_AUTHORIZE_ERR;
					exit;*/
				}
				
			}
		}
		
		//写入COOKIE
		if($useUser=='y'){
			setcookie("locationUserJKJ",$userName,time()+86400*30);
		}else{
			//删除COOKIE
			setcookie("locationUserJKJ",$userName,time()-36000);
		}
		//$arr=array("USER"=>$userName,"PWD"=>md5($userPwd));
		$__str=$userName."|".md5($userPwd);
		if($autologin=='y'){
			
			setcookie("locationAutonLoginJKJ",$__str,time()+86400*30);
		}else{
			//删除COOKIE
			setcookie("locationAutonLoginJKJ",$__str,time()-36000);
		}
		//写入session
		ExSession::getSession()->setUserByArray($_user);
		$_user["U_PWD"]=$userPwd;
		$_user["U_PWD1"]=$userPwd;
		ExSession::getSession()->setSessionByArrayAndPid($_user,$sid);
		
		ExLog::addLog($userName."手机登录", "USER_LOGIN_MOBILE");
		
		
		//$arr=array("info"=>'',"status"=>self::USER_LOGIN_OK);
		//echo json_encode($arr);
		header("Location: ?g=Mobile&m=Index&a=main");
		exit;
	}
	
	function message(){
		$this->assign("message",$this->in("title"));
		$this->assign("url",$this->in("url"));
		$this->display();
	}
	/**
    * 退出(重新登录)
    * Enter description here ...
    */	
   function logout(){
   		
   		session_destroy();
   		//setcookie("locationAutonLoginJKJ",'',time()-36000);
   		ExLog::addLog(ExSession::getSession()->getRealName()."退出登录", "MOBILE_USER_LOGOUT");
		header("Location: ?g=Mobile&m=Index&a=login");
		exit;
   }
}